package com.icesoft.wechat.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.icesoft.framework.core.controller.BaseCrudRestController;
import com.icesoft.framework.core.vo.JsonResult;
import com.icesoft.framework.core.vo.Pagination;
import com.icesoft.system.auth.entity.LoginUser;
import com.icesoft.system.entity.BaseLoginUser;
import com.icesoft.system.entity.Company;
import com.icesoft.system.service.impl.CompanyServiceImpl;
import com.icesoft.system.types.UserTypeDictionary;
import com.icesoft.system.util.SecurityUtils;
import com.icesoft.wechat.dto.WechatUserDTO;
import com.icesoft.wechat.entity.WechatUser;
import com.icesoft.wechat.vo.WechatUserVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/wechat/user")
public class WechatUserController extends BaseCrudRestController<WechatUser> {

	private final UserTypeDictionary userTypeDictionary;

	private final CompanyServiceImpl companyService;

	@GetMapping("/query")
	public JsonResult getViewObjectListMapping(WechatUserDTO entity, Pagination pagination) throws Exception {
		return super.getViewObjectList(entity, pagination, WechatUserVO.class);
	}

	/**
	 * 根据资源id查询ViewObject
	 *
	 * @param id ID
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/{id}")
	public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
		return super.getViewObject(id, WechatUserVO.class);
	}

	/**
	 * 新建
	 *
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/create")
	public JsonResult createEntityMapping(@Valid @RequestBody WechatUserDTO dto) throws Exception {
		LoginUser<BaseLoginUser> loginUser = SecurityUtils.getLoginUser();
		Company company;
		if (loginUser != null) {
			company = companyService.getEntity(loginUser.getUser().getCompanyId());
			dto.setAppId(company.getAppletAppid());
			dto.setCompanyId(loginUser.getUser().getCompanyId());
		}
		dto.setUserType(userTypeDictionary.getWechat());
		return super.createEntity(dto);
	}

	/**
	 * 更新
	 *
	 * @param dto
	 * @return JsonResult
	 * @throws Exception
	 */
	@PostMapping("/update/{id}")
	public JsonResult updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody WechatUserDTO dto) throws Exception {
		return super.updateEntity(id, dto);
	}

	/**
	 * 根据id删除资源对象
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/delete/{id}")
	public JsonResult deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
		return super.deleteEntity(id);
	}

}
